Types
Komplete Script is statically typed. Every value has a type known at compile time.
Primitive Types
| Type | Description | Literal Example |
|---|---|---|
Bool | Boolean value | true, false |
Int | 64-bit integer | 42, -7, 0 |
Float | 64-bit floating-point number | 3.14, -0.5, 1.0e10 |
String | Sequence of Unicode characters | "Hello", "" |
See Literals for the full syntax of integer, float, string, boolean, array, and map literals.
Collection Types
Array
An ordered, growable sequence of elements. The type syntax is [ElementType].
var scores: [Int] = [10, 20, 30]
var empty: [String] = []
Access elements by zero-based index. Arrays grow automatically as elements are added.
var scores: [Int] = [10, 20, 30]
var first = scores[0]
scores[1] = 99
scores.append(40)
See Array for all available properties and methods.
Map
An unordered collection of key-value pairs. The type syntax is [KeyType: ValueType].
var lookup: [String: Int] = ["a": 1, "b": 2]
var empty: [String: Float] = [:]
Access and mutate entries by key. Assigning nil to a key removes the entry. Map lookups always return an optional value.
var lookup: [String: Int] = ["a": 1, "b": 2]
var value = lookup["a"] // type: Int?
lookup["c"] = 3
lookup["a"] = nil // removes "a"
See Map for all available properties and methods.
Optional Types
An optional represents a value that may be absent. Suffix any type with ? to make it optional. The absence of a value is represented by nil.
var maybe: String? = "Hello"
maybe = nil
See Optionals for how to work with optional values.
Function Types
Function types describe the shape of a function value: its parameter types and return types.
// Takes an Int, returns a Bool
var predicate: (Int) -> (Bool) = fun (arg) { return arg > 0 }
// No parameters, no return value
var callback: () -> () = fun () {}
// Returns multiple values
var splitter: (String) -> (String, String) = fun (arg) { return arg, arg }
See Functions for how to declare and use functions.
Type Aliases
Use the type keyword to define a named alias for an existing type.
type Score = Int
type Lookup = [String: Int]
Type aliases can be exported from a module:
export type PlayerID = Int
See Modules for details on exports.